package ouc.pkere.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ouc.pkere.entity.RelationEntity;
import ouc.pkere.mapper.ExtractionMapper;
import ouc.pkere.mapper.RelationMapper;

import java.util.*;

@Service
public class RunTasksService {

    @Autowired
    private ExtractionMapper extractionMapper;

    @Autowired
    private RelationMapper relationMapper;


    public void extractFromTable(String[] tableName, Map<String, String> col, Map<String, String> primaryKey) {

        relationMapper.truncateRelation();

        long startTime = System.currentTimeMillis();

        Map<String, RelationEntity> relationMap = new HashMap<>();
        Map<String, String[]> valueIndices = new HashMap<>();

        for (int i = 0; i < tableName.length; i++) {
            System.out.println(tableName[i]);

            Map<String, Object> map = extractFromOneTable(relationMap, valueIndices, tableName[i],
                    primaryKey.get(tableName[i]), col.get(tableName[i]).split(","));
            relationMap = (Map) map.get("relation");
            valueIndices = (Map) map.get("valueIndices");
        }

//        relationRepository.saveAll(relationMap.values());
        List<RelationEntity> list = new ArrayList(relationMap.values());
        relationMapper.insertBatch(list);

        System.out.println(relationMap.size());

        relationMap.clear();
        relationMap = null;

        long endTime = System.currentTimeMillis();
        long timeSpent = endTime - startTime;
        System.out.println("time spent:" + timeSpent + "ms");


    }

    public Map<String, Object> extractFromOneTable(Map<String, RelationEntity> relationMap, Map<String, String[]> valueIndices,
                                       String tableName, String primaryKey, String[] col) {
        String col1 = col[0].trim();
        String col2 = col[1].trim();

        //表中所有数据
        List<Map<String, Object>> tableList = extractionMapper.selectAllFromOneTable(
                primaryKey.trim() + ", " + col1 + ", " + col2 + " ", tableName.trim());


        int tableListCount = tableList.size();

        for (int k = 0; k < tableListCount; k++) {
            Map<String, Object> tableData = tableList.get(k);



            //所选第一个字段不为空
            if (tableData.get(col1) != null && !String.valueOf(tableData.get(col1)).equals("null")
                    && !String.valueOf(tableData.get(col1)).equals("")) {

                String[] col1Values = String.valueOf(tableData.get(col1)).split(",");
                for (int i = 0; i < col1Values.length; i++) {

                    String value1 = col1Values[i].trim();

                    //所选第二个字段不为空
                    if (tableData.get(col2) != null && !String.valueOf(tableData.get(col2)).equals("null")
                            && !String.valueOf(tableData.get(col2)).equals("")) {

                        //第二个字段按逗号分割出一个或多个人名
                        String[] col2Values = String.valueOf(tableData.get(col2)).split(",");
                        for (int j = 0; j < col2Values.length; j++) {

                            String value2 = col2Values[j].trim();

                            //人名放入map
                            String[] valueIndex = valueIndices.putIfAbsent(value1, new String[]{tableName, String.valueOf(tableData.get(primaryKey)), primaryKey});

                            //第一个字段和第二个字段人名不相同时才将结果放入map中
                            if (!value2.equals(value1)) {

                                //人名放入map
                                valueIndices.putIfAbsent(value2, new String[]{tableName, String.valueOf(tableData.get(primaryKey)), primaryKey});

                                //如果该姓名已存在，tablevalue和rowindex为
                                if (valueIndex != null) {
                                    RelationEntity relation = new RelationEntity(
                                            (long) i, valueIndex[0], valueIndex[2], valueIndex[1], col1, value1,
                                            value1 + "-" + value2, tableName, primaryKey,
                                            String.valueOf(tableData.get(primaryKey)), col2, value2
                                    );


                                    RelationEntity relation2 = new RelationEntity(
                                            (long) i, tableName, primaryKey, String.valueOf(tableData.get(primaryKey)), col2, value2,
                                            value2 + "-" + value1, valueIndex[0], valueIndex[2], valueIndex[1], col1, value1
                                    );

                                    relationMap.put(value1 + "-" + value2, relation);
                                    relationMap.put(value2 + "-" + value1, relation2);
                                } else {
                                    RelationEntity relation = new RelationEntity(
                                            (long) i + tableListCount, tableName, primaryKey, String.valueOf(tableData.get(primaryKey)),
                                            col1, value1, value1 + "-" + value2, tableName, primaryKey,
                                            String.valueOf(tableData.get(primaryKey)), col2, value2
                                    );

                                    RelationEntity relation2 = new RelationEntity(
                                            (long) i + tableListCount, tableName, primaryKey, String.valueOf(tableData.get(primaryKey)),
                                            col2, value2, value2 + "-" + value1, tableName,
                                            primaryKey, String.valueOf(tableData.get(primaryKey)), col1, value1

                                    );
                                    relationMap.put(value1 + "-" + value2, relation);
                                    relationMap.put(value2 + "-" + value1, relation2);
                                }

                            }

                        }

                    }

                }


            }

        }

        Map<String, Object> result = new HashMap<>();
        result.put("relation", relationMap);
        result.put("valueIndices", valueIndices);
        tableList.clear();
        tableList = null;
        return result;

    }


}

